package cz.muni.fi.pa165.travelagency.web;

import com.samaxes.stripes.ejb3.EJBBean;
import cz.muni.fi.pa165.travelagency.api.entities.IPackageTour;
import cz.muni.fi.pa165.travelagency.api.services.IPackageTourService;
import cz.muni.fi.pa165.travelagency.dto.PackageTourDTO;
import java.util.List;
import javax.ejb.EJB;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Lukas Jungmann
 */
@UrlBinding("/ptour/{$event}/{ptour.id}")
@EJB(name = "ptourService", beanInterface = IPackageTourService.class)
public class PackageTourActionBean implements ActionBean {

    private final static Logger log = LoggerFactory.getLogger(PackageTourActionBean.class);
    private ActionBeanContext context;
    @ValidateNestedProperties(value = {
        @Validate(on = {"add", "save"}, field = "name", required = true),
        @Validate(on = {"add", "save"}, field = "destination", required = true),
        @Validate(on = {"add", "save"}, field = "pricePerPerson", required = true, minvalue = 0),
        @Validate(on = {"add", "save"}, field = "start", required = true),
        @Validate(on = {"add", "save"}, field = "end", required = true),
        @Validate(on = {"add", "save"}, field = "capacity", required = true, minvalue = 1),
        @Validate(on = {"add", "save"}, field = "description", required = true)
    })
    private PackageTourDTO ptour;
    @EJBBean("java:comp/env/ptourService")
    private IPackageTourService service;

    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }

    public List<IPackageTour> getPtours() {
        return service.findAll();
    }

    public PackageTourDTO getPtour() {
        return ptour;
    }

    public void setPtour(PackageTourDTO ptour) {
        this.ptour = ptour;
    }

    @DefaultHandler
    public Resolution all() {
        return new ForwardResolution("/ptour_show.jsp");
    }

    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit"})
    public void loadBookFromDatabase() {
        String ids = context.getRequest().getParameter("ptour.id");
        if (ids == null) {
            return;
        }
        ptour = (PackageTourDTO) service.getPackageTour(Long.parseLong(ids));
    }

    public Resolution add() {
        service.createPackageTour(ptour);
        return new RedirectResolution(this.getClass(), "all");
    }

    public Resolution delete() {
        service.deletePackageTour(ptour);
        return new RedirectResolution(this.getClass(), "all");
    }

    public Resolution edit() {
        return new ForwardResolution("/ptour_edit.jsp");
    }

    public Resolution save() {
        service.updatePackageTour(ptour);
        return new RedirectResolution(this.getClass(), "all");
    }

    public Resolution cancel() {
        return new ForwardResolution("/ptour_show.jsp");
    }
    
    public Resolution availableTours() {
        String ids = context.getRequest().getParameter("ptour.id");
        if (ids == null) {
           return new ForwardResolution("/ptour_show_tours.jsp");
        }
        ptour = (PackageTourDTO) service.getPackageTour(Long.parseLong(ids));
        
        return new ForwardResolution("/ptour_show_tours.jsp");
    }
}
